﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows;

namespace 数据绑定
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlHelper sh = new SqlHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();

            }
        }

        private void Bind()
        {
            string sql = "select * from StudentInfo";
            GridView1.DataSource = sh.Get(sql, null);
            GridView1.DataKeyNames = new string[] { "stu_id" };//给之后增删改一个索引
            GridView1.DataBind();
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            string name = UserName.Text;
            string pwd = Password.Text;
            string email = Email.Text;
            string intro = Intro.Text;

            string sql = "insert into StudentInfo(stu_name,password,email,intro) values(@name,@pwd,@email,@intro)";

            SqlParameter[] parms =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@email",email),
                new SqlParameter("@intro",intro)
            };

            if (sh.Execute(sql, parms))
            {
                Bind();
            }
            else
            {
                Response.Write("注册失败");
            }


        }

        protected void Refer_Click(object sender, EventArgs e)
        {
            string name = ReferByName.Text;
            string sql = "select * from StudentInfo where stu_name like '%'+@name+'%'";

            SqlParameter[] pars =
            {
                new SqlParameter("@name",name)
            };

            GridView1.DataSource = sh.Get(sql, pars);

            GridView1.DataBind();
        }



        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string name = (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).ToString();
            string pwd = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).ToString();
            string email = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).ToString();
            string intro = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).ToString();

            string sql = "update StudentInfo set stu_name = @name, password = @pwd,email = @email,intro = @intro where stu_id=@id";

            SqlParameter[] parms =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@email",email),
                new SqlParameter("@intro",intro),
                new SqlParameter("@id",id)

            };

            if (sh.Execute(sql, parms))
            {
                Response.Write("编辑成功");
            }
            else
            {
                Response.Write("编辑失败");
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            //string name = (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).ToString();
            //string pwd = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).ToString();
            //string email = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).ToString();
            //string intro = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).ToString();

            string sql = "delete StudentInfo where stu_id=@id";
            SqlParameter[] parms =
            {
                new SqlParameter("@id",id)
            };

            if (sh.Execute(sql, parms))
            {
                Response.Write("删除成功");
            }
            else
            {
                Response.Write("删除失败");
            }

            Bind();
        }
    }
}